package com.ojoin.trade.common_core.web.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.web.dao.BaseDao;
import com.ojoin.trade.common.web.service.impl.BaseServiceImpl;
import com.ojoin.trade.common_core.web.dao.TbMerchantuserFinaccountDao;
import com.ojoin.trade.common_core.web.domain.TbMerchantuserFinaccount;
import com.ojoin.trade.common_core.web.domain.TbMerchantuserFinaccountVo;
import com.ojoin.trade.common_core.web.service.TbMerchantuserFinaccountService;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-10-26
 * Time: 下午5:21
 * To change this template use File | Settings | File Templates.
 */
@Service
public class TbMerchantuserFinaccountServiceImpl extends BaseServiceImpl<TbMerchantuserFinaccount> implements TbMerchantuserFinaccountService {

    @Autowired
    private TbMerchantuserFinaccountDao mdao;
    @Override
    protected BaseDao<TbMerchantuserFinaccount> getBaseDao() {
        return mdao;
    }

    private Logger log = LoggerFactory
			.getLogger(TbMerchantuserFinaccountServiceImpl.class);

    @Override
    public void insertTbMerchantuserFinaccount(TbMerchantuserFinaccount accountvo) throws BusinessException {
    	Integer idOfTbMerchantUser = accountvo.getIdOfTbMerchantUser();
    	Integer businessType = accountvo.getBusinessType();
    	String accountType = accountvo.getAccountType();
    	if (idOfTbMerchantUser == null)
    		throw new BusinessException("商户结算户信息的九盈id不可为空.");
    	
    	if (businessType == null)
    		throw new BusinessException("商户结算户信息的业务类型不可为空.");
    	
    	if (StringUtils.isEmpty(accountType))
    		throw new BusinessException("商户结算户信息的账户类型不可为空.");
    	
    	
    	TbMerchantuserFinaccount finaccount = this.queryFinaccount(
    			idOfTbMerchantUser,
    			businessType,
    			accountType);
    	
        if(CommonUtils.isEmpty(finaccount)) {
        	log.info("新增结算户数据. joinuserid->" + idOfTbMerchantUser + ", busitype->" + businessType + ", accounttype->" + accountType);
        	((TbMerchantuserFinaccountDao)this.getBaseDao()).insert(accountvo);
        } else {
        	accountvo.setId(finaccount.getId());
        	
        	log.info("更新结算户数据. joinuserid->" + idOfTbMerchantUser + ", busitype->" + businessType + ", accounttype->" + accountType);
        	((TbMerchantuserFinaccountDao)this.getBaseDao()).updateById(accountvo);
        }
    }

    @Override
    public List<TbMerchantuserFinaccount> queryFinaccountByJoinid(int idOfTbMerchantUser, int businessType) throws BusinessException {
        return ((TbMerchantuserFinaccountDao)this.getBaseDao()).queryFinaccountByJoinid(idOfTbMerchantUser,businessType);
    }

    @Override
    public List<TbMerchantuserFinaccountVo> queryFinaccountByType(int businessType,String accountType) throws BusinessException {
        return ((TbMerchantuserFinaccountDao)this.getBaseDao()).queryFinaccountByType(businessType,accountType);
    }

    @Override
    public TbMerchantuserFinaccount queryFinaccount(Integer idOfTbMerchantUser, Integer businessType, String accountType)throws BusinessException{
         return ((TbMerchantuserFinaccountDao)this.getBaseDao()).queryFinaccount(idOfTbMerchantUser,businessType,accountType);
    }

    @Override
    public List<TbMerchantuserFinaccount> queryFinaccountByJoinidAndAcctype(Integer idOfTbMerchantUser, String acccountType) throws BusinessException {
        return   ((TbMerchantuserFinaccountDao)this.getBaseDao()).queryFinaccountByJoinidAndAcctype(idOfTbMerchantUser, acccountType);
    }
}
